package com.global.college.ui.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.global.college.R;
import com.global.college.adapter.CourseExpandAdapter;
import com.global.college.api.ProgressSubscriber;
import com.global.college.api.RequestClient;
import com.global.college.base.BaseFragment;
import com.global.college.data.net.CourseVideosBean;
import com.global.college.ui.activity.CourseDetailActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * 课程-目录
 */
public class CourseDetailFragment extends BaseFragment {
    @BindView(R.id.recycler_course)
    RecyclerView recyclerCourse;

    protected static final String ARG_PARAM1 = "param1";
    protected static final String ARG_PARAM2 = "param2";
    protected String classId;
    protected String mParam2;

    private CourseExpandAdapter courseExpandAdapter;

    public CourseDetailFragment() {
    }

    public static CourseDetailFragment newInstance(String param1, String param2) {
        CourseDetailFragment fragment = new CourseDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            classId = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_course_detail;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        recyclerCourse.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    protected void initData() {
        getStudyPath();
    }

    /**
     * 获取目录
     */
    private void getStudyPath() {
        RequestClient.getInstance().getCourseVideos(classId).subscribe(new ProgressSubscriber<CourseVideosBean[]>(mActivity, true) {
            @Override
            public void onNext(CourseVideosBean[] model) {
                List<CourseVideosBean> courseVideosBeanList = Arrays.asList(model);

                if (courseVideosBeanList.get(0).getCatalogModelList() != null && courseVideosBeanList.get(0).getCatalogModelList().size() > 0) {
                    //三层
                    setThreeAdapter(courseVideosBeanList);
                    ((CourseDetailActivity)mActivity).getVideoHistory(courseVideosBeanList.get(0).getCatalogModelList().get(0).getVideoModelList().get(0).getVideoId());
                } else {
                    //两层
                    setTwoAdapter(courseVideosBeanList);
                    ((CourseDetailActivity)mActivity).getVideoHistory(courseVideosBeanList.get(0).getVideoModelList().get(0).getVideoId());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    private void setTwoAdapter(List<CourseVideosBean> courseVideosBeanList) {
        List data = new ArrayList<>();
        for (CourseVideosBean firstBean : courseVideosBeanList) {
            for (CourseVideosBean.CatalogModelListBean.VideoModelBean videoModelBean : firstBean.getVideoModelList()) {
                firstBean.addSubItem(videoModelBean);
            }
            data.add(firstBean);
        }
        courseExpandAdapter = new CourseExpandAdapter(data);
        courseExpandAdapter.bindToRecyclerView(recyclerCourse);
    }

    private void setThreeAdapter(List<CourseVideosBean> courseVideosBeanList) {
        List data = new ArrayList<>();
        for (CourseVideosBean firstBean : courseVideosBeanList) {
            for (CourseVideosBean.CatalogModelListBean secondBean : firstBean.getCatalogModelList()) {
                for (CourseVideosBean.CatalogModelListBean.VideoModelBean videoModelBean : secondBean.getVideoModelList()) {
                    secondBean.addSubItem(videoModelBean);
                }
                firstBean.addSubItem(secondBean);
            }
            data.add(firstBean);
        }
        courseExpandAdapter = new CourseExpandAdapter(data);
        courseExpandAdapter.bindToRecyclerView(recyclerCourse);
    }

    public void setCurrentVideo(String videoId){
        courseExpandAdapter.setCurrentVideoId(videoId);
    }


}
